/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.monitor.profile.structure;

import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.monitor.profile.statistics.structure
 * @author:LiBo/Alex
 * @create-date:2019-08-07 11:09
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
public class ElementStack<T> {


    private AtomicInteger elementCount = new AtomicInteger(0);

    private static final int DEFAULT_INIT_SIZE = 16;

    private Object[] elementData = new Object[DEFAULT_INIT_SIZE];

    @Getter
    private volatile int capacity = DEFAULT_INIT_SIZE;

    /**
     * 构造器01
     */
    public ElementStack() {

    }

    /**
     * 构造器02
     * @param initalizeSize
     */
    public ElementStack(int initalizeSize) {
        autoExtenseELementData(initalizeSize);
    }

    /**
     * 自动化扩展堆栈的大小操作
     * @param size
     */
    public void autoExtenseELementData(int size){
        if(size <= 0) {
            throw new IllegalArgumentException("传入的初始化数值不可以小于等于0");
        }
        while(capacity < size){
            capacity = capacity << 1;
        }
        elementData = Arrays.copyOf(elementData,capacity);
    }

    /**
     * push 推入一个元素到栈顶
     * @param param
     */
    public T push(T param){
        autoExtenseELementData(elementCount.get()+1);
        elementData[elementCount.getAndIncrement()] = param;
        return param;
    }

    /**
     * pop退出元素
     * @return
     */
    public T pop(){
        T element = peek();
        return remove(elementCount.get() - 1);
    }

    /**
     * peek输出对象
      * @return
     */
    public T peek(){
       if(elementCount.get() == 0){
           throw new IllegalStateException("is not any element in the stack container");
       }
       return get(elementCount.get()-1);
    }


    /**
     * get获取index索引信息
     * @param index
     * @return
     */
    public T get(int index){
        if(index >= elementCount.get()){
            throw new IllegalStateException("the index element is greate than the container bound");
        }
        return (T) elementData[index];
    }

    /**
     * remove移除相关的下标的元素
     * @param index
     * @return
     */
    public T remove(int index){
        //校验区间范围
        if(index <= 0 || index > elementCount.get()){
            throw new IllegalStateException("超过区间范畴!");
        }
        //移动的元素个数
        int moveNum = elementCount.get() - index - 1;
        Object elementObj = elementData[index];
        //判断算法
        if(moveNum > 0){
            System.arraycopy(elementData,index+1,elementData,index,moveNum);
        }
        elementCount.decrementAndGet();
        elementData[elementCount.intValue()] = null;
        return (T) elementObj;
    }

    /**
     * 获取元素长度或者大小
     * @return
     */
    public int size(){
        return elementCount.get();
    }

    /**
     * 销毁功能-销毁功能操作
     */
    public void destroy(){
        elementData = new Object[DEFAULT_INIT_SIZE];
        capacity = DEFAULT_INIT_SIZE;
        elementCount = new AtomicInteger(0);
    }

    /**
     * 清理功能-将清理操作
     * 元素数据都会重置为null
     */
    public void reset(){
        Stream.of(elementData).forEach(param->param = null);
    }


    public static void main(String[] args){
        ElementStack<Integer> elementStack = new ElementStack<>();
        elementStack.push(12);
        System.out.println(elementStack.peek());
    }

}
